package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.RecruitListData;
import com.wbao.dianniu.utils.DateUtils;

/**
 * 岗位列表（公司招聘和简历投递）
 *
 * @author yejianjun
 */
public class RecruitListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<RecruitListData> mList;
    private String split = " | ";

    public RecruitListAdapter(Context context) {
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (null == mList) {
            mList = new ArrayList<RecruitListData>();
        }
    }

    public void addData(List<RecruitListData> list) {
        if (null == list)
            return;
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
    }

    public void setData(List<RecruitListData> list) {
        mList = list;
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public List<RecruitListData> getList() {
        return mList;
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.recruit_list_item, null);
            holder = new GridHolder();
            holder.postTV = convertView.findViewById(R.id.recruit_list_job);
            holder.salaryTV = convertView.findViewById(R.id.recruit_list_salary);
            holder.baseInfoTV = convertView.findViewById(R.id.recruit_list_baseInfo);
            holder.timeTV = convertView.findViewById(R.id.recruit_list_time);
            convertView.setTag(holder);
        } else {
            holder = (GridHolder) convertView.getTag();
        }
        RecruitListData data = mList.get(position);
        if (null != data) {
            holder.postTV.setText(data.getPost() == null ? "" : data.getPost());
            holder.salaryTV.setText(data.getSalary() == null ? "" : "" + data.getSalary());
            StringBuffer stringBuffer = new StringBuffer();
            boolean fistIsNull = false;
            boolean secondIsNull = false;
            if (null != data.getAddress()) {
                stringBuffer.append(data.getAddress());
            } else {
                fistIsNull = true;
            }
            if (null != data.getExperience()) {
                if (!fistIsNull) {
                    stringBuffer.append(split);
                }
                stringBuffer.append(data.getExperience());
            } else {
                secondIsNull = true;
            }
            if (null != data.getEducation()) {
                if (!secondIsNull) {
                    stringBuffer.append(split);
                }
                stringBuffer.append(data.getEducation());
            }
            holder.baseInfoTV.setText(stringBuffer);
            holder.timeTV.setText(DateUtils.getFormatChatTime(data.getLastUpdateDate()));
        }
        return convertView;
    }

    public class GridHolder {
        public TextView postTV;
        public TextView salaryTV;
        public TextView baseInfoTV;
        public TextView timeTV;
    }

}
